package com.feng.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.feng.dao.PhotoDao;
import com.feng.entity.Photo;
import com.feng.service.IPhotoService;
import com.feng.util.CommonUtils;

@Service
public class PhotoServiceImpl implements IPhotoService{

	@Value("${web.upload-path}")
	private String uploadPath;
	
	@Autowired
	private PhotoDao photoDao;
	
	@Override
	@Transactional
	public void uploadPhoto(MultipartFile file,Photo photo) throws IllegalStateException, IOException {
		String fileName = file.getOriginalFilename();
		int index = fileName.lastIndexOf(".");
		String filePostfix = fileName.substring(index);
		String newfileName = CommonUtils.getUUID() + filePostfix;
		file.transferTo(new File(uploadPath + "file/img/" + newfileName));
			
		photo.setId(CommonUtils.getUUID());
		photo.setUploaddate(new Date());
		photo.setUuidname(newfileName);
		photo.setPath("/file/img/" + newfileName);
		photoDao.insertPhoto(photo);
	}

	@Override
	public List<Photo> findAllPhotos() {
		return photoDao.findAllPhotos();
	}

}
